#include"Queue.h"
void QInit(Queue* pst)
{
	assert(pst);
	pst->phead = pst->ptail = NULL;
	pst->size = 0;
}
void QDestroy(Queue* pst)
{
	assert(pst);
	SLNode* cur = pst->phead;
	while (cur)
	{
		
		SLNode*next= cur->next;
		free(cur);
		
		cur = next;
	}
	pst->phead = pst->ptail = NULL;
	pst->size = 0;
}
void QPush(Queue* pst, QDataType x)
{
	assert(pst);
	SLNode* newnode = (SLNode*)malloc(sizeof(SLNode));
	if (newnode == NULL)
	{
		perror("malloc fail");
		return;
	}
	newnode->val = x;
	newnode->next = NULL;
	if (pst->ptail == NULL)
	{	
		pst->ptail =pst->phead= newnode;
	}
	else
	{
		pst->ptail->next = newnode;
		pst->ptail = newnode;
	}
	pst->size++;
}
void QPop(Queue* pst)
{
	assert(pst);
	assert(pst->phead);
	SLNode* cur = pst->phead;
	pst->phead = pst->phead->next;
	free(cur);
	cur = NULL;
	if (pst->phead == NULL)
	{
		pst->ptail = NULL;
	}
	pst->size--;
}
QDataType QFront(Queue* pst)
{
	assert(pst);
	assert(pst->phead);
	return pst->phead->val;
}
QDataType QBack(Queue* pst)
{
	assert(pst);
	assert(pst->ptail);
	return pst->ptail->val;
}
bool QEmpty(Queue* pst)
{
	assert(pst);
	return pst->phead == NULL;
}
int QSize(Queue* pst)
{
	assert(pst);
	return pst->size;
}

